import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { SysRole } from '#/api/user/sysrole';

import { useAccess } from '@vben/access';

import { z } from '#/adapter/form';
import { COMMON_DICT, ROLE_DICT } from '#/api';
import { $t } from '#/locales';

const { hasAccessByCodes } = useAccess();

/**
 * 查询参数
 */
export function querySchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'roleName',
      label: $t('role.name'),
    },
    {
      component: 'ApiSelect',
      componentProps: {
        buttonStyle: 'solid',
        clearable: true,
        options: [
          { label: $t('common.enabled'), value: '0' },
          { label: $t('common.disabled'), value: '1' },
        ],
        optionType: 'button',
      },
      defaultValue: null,
      fieldName: 'status',
      label: $t('common.status'),
    },
  ];
}

/**
 * 获取编辑表单的字段配置。如果没有使用多语言，可以直接export一个数组常量
 */
export function formSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'roleName',
      label: $t('role.name'),
      rules: z
        .string()
        .min(2, $t('ui.formRules.minLength', [$t('role.name'), 2]))
        .max(20, $t('ui.formRules.maxLength', [$t('role.name'), 20])),
    },
    {
      component: 'InputNumber',
      fieldName: 'roleSort',
      label: $t('common.sort'),
      rules: z
        .number()
        .gte(0, $t('ui.formRules.minLength', [$t('common.sort'), 0]))
        .lte(100, $t('ui.formRules.maxLength', [$t('common.sort'), 100])),
      defaultValue: 0,
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: COMMON_DICT.STATUS_ENABLE },
          { label: $t('common.disabled'), value: COMMON_DICT.STATUS_DISABLE },
        ],
        optionType: 'button',
      },
      defaultValue: COMMON_DICT.STATUS_ENABLE,
      fieldName: 'status',
      label: $t('common.status'),
    },
    {
      component: 'Input',
      fieldName: 'menuIds',
      label: $t('menu.perms'),
    },
    {
      component: 'Select',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('role.dataScope_all'), value: ROLE_DICT.DATA_SCOPE_ALL },
          {
            label: $t('role.dataScope_custom'),
            value: ROLE_DICT.DATA_SCOPE_CUSTOM,
          },
          {
            label: $t('role.dataScope_dept'),
            value: ROLE_DICT.DATA_SCOPE_DEPT,
          },
          {
            label: $t('role.dataScope_dept_child'),
            value: ROLE_DICT.DATA_SCOPE_DEPT_AND_CHILD,
          },
        ],
        optionType: 'button',
      },
      defaultValue: ROLE_DICT.DATA_SCOPE_ALL,
      fieldName: 'dataScope',
      label: $t('role.dataScope'),
    },
    {
      component: 'Input',
      fieldName: 'deptIds',
      label: $t('dept.dept'),
      componentProps: {
        border: true,
      },
      dependencies: {
        show: (row) => {
          return row.dataScope === ROLE_DICT.DATA_SCOPE_CUSTOM;
        },
        triggerFields: ['dataScope'],
      },
      modelPropName: 'modelValue',
    },
  ];
}

/**
 * 获取表格列配置
 * @description 使用函数的形式返回列数据而不是直接export一个Array常量，是为了响应语言切换时重新翻译表头
 * @param onActionClick 表格操作按钮点击事件
 */
export function columnsSchema<T>(
  onStatusChange?: (
    newStatus: string,
    row: T,
  ) => PromiseLike<boolean | undefined>,
  onActionClick?: OnActionClickFn<SysRole>,
): VxeTableGridOptions<SysRole>['columns'] {
  return [
    { title: '序号', type: 'seq', width: 50 },
    {
      field: 'roleName',
      title: $t('role.name'),
      width: 200,
    },
    {
      field: 'roleSort',
      title: $t('common.sort'),
      width: 150,
    },
    {
      field: 'dataScope',
      cellRender: {
        name: 'CellTag',
        options: [
          { label: $t('role.dataScope_all'), value: ROLE_DICT.DATA_SCOPE_ALL },
          {
            label: $t('role.dataScope_custom'),
            value: ROLE_DICT.DATA_SCOPE_CUSTOM,
          },
          {
            label: $t('role.dataScope_dept'),
            value: ROLE_DICT.DATA_SCOPE_DEPT,
          },
          {
            label: $t('role.dataScope_dept_child'),
            value: ROLE_DICT.DATA_SCOPE_DEPT_AND_CHILD,
          },
        ],
      },
      title: $t('role.dataScope'),
      width: 200,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: 'CellSwitch',
      },
      field: 'status',
      title: $t('common.status'),
      width: 150,
    },
    {
      field: 'createAt',
      title: $t('common.createTime'),
      width: 300,
    },
    {
      cellRender: {
        attrs: {
          nameField: 'roleName',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: () => {
              return hasAccessByCodes(['sys_role:edit']);
            },
          },
          {
            code: 'delete',
            text: true,
            show: () => {
              return hasAccessByCodes(['sys_role:remove']);
            },
          },
        ],
      },
      field: 'operation',
      fixed: 'right',
      align: 'center',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('page.action.action'),
    },
  ];
}
